/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2024-03-24
 * Time: 15:21
 */
public class BlockingQueue {
    private String[] elems = null;
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    private Object locker = new Object();

    public BlockingQueue(int capacity) {
        elems = new String[capacity];
    }

    public void put(String elem) throws InterruptedException {
        synchronized (locker) {
            while(size >= elem.length()) {
                locker.wait();
            }

            elems[tail] = elem;
            tail++;
            if(tail >= elem.length()) {
                tail = 0;
            }
            size++;

            locker.notify();
        }
    }

    public String take() throws InterruptedException {
        String elem = null;
        synchronized (locker){
            while(size == 0) {
                locker.wait();
            }

            elem = elems[size-1];
            head++;
            if (head >= elems.length) {
                head = 0;
            }

            size--;

            locker.notify();
        }
        return elem;

    }


}
